package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description RoleServiceImpl
 * @Author loe
 * @Date 2024-07-15
 */

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 分页查询
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @Transactional
    public PageResponse<RoleVo> limitSearch(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> page = roleMapper.limitSearch(role);
        return PageResponse.of(page, RoleVo.class);
    }

    /**
     * 角色添加
     * @param roleDto
     */
    @Override
    public void insertRole(RoleDto roleDto) {
        roleMapper.insertSelective(BeanUtil.toBean(roleDto, Role.class));
    }

    /**
     * 根据角色查询选中的资源数据
     * @param roleId
     * @return
     */
    @Override
    public List<String> getResourceNoByRoleId(Long roleId) {
        return roleResourceMapper.getResourceNoByRoleId(roleId);
    }

    /**
     * 角色修改
     * @param roleDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        // 判断该角色是否绑定用户
        // if (role.getDataState() == null) {
        //     userRoleMapper.up
        // }

        // int i = userRoleMapper.checkIsUser(roleDto.getId());
        // if (i != 0 && roleDto.getDataState().equals(SuperConstant.DATA_STATE_1)) {
        //     throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        // }

        roleMapper.updateByPrimaryKeySelective(role);

        // 判断资源no是否为空
        if (roleDto.getCheckedResourceNos() == null) {
            return;
        }
        // 不为空先删除, 再修改
        roleResourceMapper.deleteByRoleId(roleDto.getId());
        // 修改
        List<RoleResource> roleResourceList = new ArrayList<>();
        for (String resourceNo : roleDto.getCheckedResourceNos()) {
            roleResourceList.add(RoleResource.builder()
                    .roleId(roleDto.getId())
                    .resourceNo(resourceNo)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build());
        }
        // 插入
        roleResourceMapper.batchInsert(roleResourceList);
    }

    /**
     * 角色删除
     * @param roleId
     */
    @Override
    @Transactional
    public void deleteRole(String roleId) {
        Long lRoleId = Long.valueOf(roleId);
        // 如果绑定用户不能删除
        if (userRoleMapper.checkIsUser(lRoleId) != 0) {
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        // 如果是启用状态不能删除
        int ret = roleMapper.deleteByPrimaryKey(lRoleId);
        if (ret == 0) {
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        // 删除角色表后, 再删除角色资源关联表
        roleResourceMapper.deleteByRoleId(lRoleId);
    }

    /**
     * 角色下拉列表
     * @return
     */
    @Override
    public List<RoleVo> getRoleList() {
        return roleMapper.getRoleVoList();
    }
}
